﻿<cfscript>
/** 
* MachII Caching Frameworks EhCache
* 
* @hint "A caching strategy that uses a cfml engine's native EhCache."
* 
*/ 

component 
	extends="MachII.caching.strategies.AbstractCacheStrategy" 
	displayname="public.caching.strategies.EhCache" 
	output=false 
	accessors=true 
	{
		
	/* PROPERTIES */
	property type="string" name="timespanString" default="0,1,0,0";
	property type="string" name="idleTimespanString" default="0,0,30,0";
	property type="numeric" name="timespan";
	property type="numeric" name="idleTimespan";
	
	/* INITIALIZATION / CONFIGURATION */
	
	/**
	* @hint "Configures the strategy."
	*/
	public void function configure() output=false {
		
		if ( isParameterDefined("timespan") AND getAssert().isTrue(listLen(getParameter("timespan")) eq 4, "Invalid timespan of '#getParameter("timespan")#'.", "Timespan must be set to a list of 4 numbers (days, hours, minutes, seconds).") ) {
			setTimespanString(getParameter("timespan"));
		}
		else {
			setTimespanString("0,1,0,0");
		}
		
		if (isParameterDefined("idleTimespan") AND getAssert().isTrue(listLen(getParameter("idleTimespan")) eq 4, "Invalid idleTimespan of '#getParameter("idleTimespan")#'.", "IdleTimespan must be set to a list of 4 numbers (days, hours, minutes, seconds).")) {
			setIdleTimespanString(getParameter("idleTimespan"));
		}
		else {
			setIdleTimespanString("0,0,30,0");
		}

		settimespan( createTimeSpan( listGetAt(getTimespanString(), 1), listGetAt(getTimespanString(), 2),listGetAt(getTimespanString(), 3), listGetAt(getTimespanString(), 4) ) );
		setidleTimespan( createTimeSpan( listGetAt(getIdleTimespanString(), 1), listGetAt(getIdleTimespanString(), 2),listGetAt(getIdleTimespanString(), 3), listGetAt(getIdleTimespanString(), 4) ) );

	}
	
	/* PUBLIC FUNCTIONS */
	
	/**
	* @hint "Gets data from the cache by key. Returns null if the key is not in the cache."
	* 
	* @key "Cache Object KeyId"
	*/
	public function get( required string key ) output=false {
		
		var hashedKey = hashKey(arguments.key);
		
		if ( CGI.REMOTE_ADDR eq "127.0.0.1" ) {
			writeLog( type="Information", file="debug", text="[cache] Cache Read #lCase( arguments.key )#");
		}

		return cacheGet( hashedKey, "matrixFramework" );
	}
	
	/**
	* @hint "Puts data into the cache by key."
	* 
	* @key "Cache Object KeyId"
	* @data "Cache Content"
	*/
	public void function put( required string key, required data ) output=false {
		
		var hashedKey = hashKey(arguments.key);
		
		if ( CGI.REMOTE_ADDR eq "127.0.0.1" ) {
			writeLog( type="Information", file="debug", text="[cache] Cache Created #lCase( arguments.key )#");
		}

		cachePut( hashedKey, arguments.data, getTimespan(), getIdleTimespan(), "matrixFramework" );
	}
	
	/**
	* @hint "Removes data from the cache by key."
	* 
	* @key "Cache Object KeyId"
	*/
	public void function remove( required string key ) output=false {
		
		var hashedKey = hashKey(arguments.key);

		if ( CGI.REMOTE_ADDR eq "127.0.0.1" ) {
			writeLog( type="Information", file="debug", text="[cache] Cache Remove #lCase( arguments.key )#");
		}
		
		cacheRemove( hashedKey, false, "matrixFramework" );
	}
	
	/**
	* @hint "Flushes all elements from the cache for Adobe ColdFusion."
	*/
	public void function flush() output=false {
		
		var cachedKeysArray = cacheGetAllIds( "matrixFramework" );
		
		for ( var hashedKey in cachedKeysArray ) {
			cacheRemove( hashedKey, false, "matrixFramework" );
		}
	}
	
	/**
	* @hint "Checkes if a key exists in the cache."
	* 
	* @key "Cache Object KeyId"
	*/
	public boolean function keyExists( required string key ) output=false {
		
		var hashedKey = hashKey( arguments.key );
		
		if ( CGI.REMOTE_ADDR eq "127.0.0.1" ) {
			writeLog( type="Information", file="debug", text="[cache] Cache Check Exists #lCase( arguments.key )#");
		}

		return isNull( cacheGet( hashedKey, "matrixFramework" ) );
	}
	
	/* PROTECTED FUNCTIONS - UTIL */
	
	/**
	* @hint "Creates a hashed version of the passed key."
	* 
	* @key "Cache Object KeyId"
	*/
	private string function hashKey( required string key ) output=false {
		return hash( uCase( trim( arguments.key ) ), "MD5" );
	}
	
}

</cfscript>